package com.example.xui_study;


import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.adapter.SmartRecyclerAdapter;
import com.scwang.smartrefresh.layout.adapter.SmartViewHolder;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.xuexiang.xui.utils.WidgetUtils;

import java.lang.reflect.Constructor;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.layout.simple_list_item_2;

public class DropDownRefreshActivity extends BaseActivity implements SmartViewHolder.OnItemClickListener{

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drop_down_refresh);
        ButterKnife.bind(this);
        WidgetUtils.initRecyclerView(mRecyclerView);

        //初始化控件
        mRecyclerView.setAdapter(new SmartRecyclerAdapter<Item>(Arrays.asList(Item.values()), simple_list_item_2, this) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, Item model, int position) {
                holder.text(android.R.id.text1, model.name());
                holder.text(android.R.id.text2, model.nameId);
                holder.textColorId(android.R.id.text2, R.color.xui_config_color_light_blue_gray);
            }
        });

//        设置下拉刷新为  贝塞尔圆圈
        RefreshHeader header = getRefreshHeader(Item.values()[0]);
        if (header != null) {
            mRefreshLayout.setRefreshHeader(header);
        }
    }

    /**
     * 点击
     *
     * @param itemView
     * @param position
     */
    @Override
    public void onItemClick(View itemView, int position) {
        if (!RefreshState.None.equals(mRefreshLayout.getState())) {
            return;
        }
        Item item = Item.values()[position];
        RefreshHeader header = getRefreshHeader(item);
        if (header != null) {
            mRefreshLayout.setRefreshHeader(header);
            mRefreshLayout.autoRefresh();
        }
    }

    private RefreshHeader getRefreshHeader(Item item) {
        try {
            Class<?> headerClass = Class.forName("com.scwang.smartrefresh.header." + item.name());
            Constructor<?> constructor = headerClass.getConstructor(Context.class);
            return (RefreshHeader) constructor.newInstance(DropDownRefreshActivity.this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private enum Item {
        BezierCircleHeader(R.string.item_head_style_bezier_circle),
        DeliveryHeader(R.string.item_head_style_delivery),
        DropBoxHeader(R.string.item_head_style_drop_box),
        WaveSwipeHeader(R.string.item_head_style_wave_swipe);
        public int nameId;

        Item(@StringRes int nameId) {
            this.nameId = nameId;
        }
    }

    @OnClick({R.id.bt_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_back:
                onBackPressed();
                break;
            default:
                break;
        }
    }

}
